# Copyright (c) 2023 Presto Labs Pte. Ltd.
# Author: leon

from typing import List, Dict

from absl import app, flags

from coin.exchange.uniswap_v2.kr_rest.native_private_client_v2 import UniswapV2NativePrivateClientV2
from coin.exchange.uniswap_v3.kr_rest.native_private_client_v2 import UniswapV3NativePrivateClientV2

FLAGS = flags.FLAGS


def approve_submit_order(
    blockchain_name: str,
    mea: str,
    wallet_address: str,
    symbol: str,
) -> bool:
    if mea == 'Spot.Uniswap.v2':
        npc = UniswapV2NativePrivateClientV2()
        return npc.approve_submit_order(
            wallet_address=wallet_address,
            symbol=symbol)
    elif mea == 'Spot.Uniswap.v3':
        npc = UniswapV3NativePrivateClientV2(blockchain_name=blockchain_name)
        return npc.approve_submit_order(
            wallet_address=wallet_address,
            symbol=symbol,
        )
    elif mea == 'Spot.Uniswap.v3-arbitrum':
        npc = UniswapV3NativePrivateClientV2(blockchain_name=blockchain_name)
        return npc.approve_submit_order(
            wallet_address=wallet_address,
            symbol=symbol,
        )
    else:
        raise NotImplementedError(blockchain_name, mea)


def approve_provide_liquidity(
    blockchain_name: str,
    mea: str,
    wallet_address: str,
    symbol: str,
) -> bool:
    if mea == 'Spot.Uniswap.v2':
        raise NotImplementedError(blockchain_name, mea)
    elif mea == 'Spot.Uniswap.v3':
        npc = UniswapV3NativePrivateClientV2(blockchain_name=blockchain_name)
        return npc.approve_provide_liquidity(
            wallet_address=wallet_address,
            symbol=symbol)
    elif mea == 'Spot.Uniswap.v3-arbitrum':
        npc = UniswapV3NativePrivateClientV2(blockchain_name=blockchain_name)
        return npc.approve_provide_liquidity(
            wallet_address=wallet_address,
            symbol=symbol)
    else:
        raise NotImplementedError(blockchain_name, mea)


def main(_):
    blockchain_name = FLAGS.blockchain_name
    mea = FLAGS.mea
    assert mea, '--mea must be specified.'
    wallet_address = FLAGS.wallet_address
    assert wallet_address, '--wallet_address must be specified.'
    symbol = FLAGS.symbol
    assert symbol, '--symbol must be specified.'
    approve_type = FLAGS.approve_type
    assert approve_type, '--approve_type must be specified.'

    if approve_type == 'swap':
        print(approve_submit_order(blockchain_name, mea, wallet_address, symbol))
    elif approve_type == 'lp':
        print(approve_provide_liquidity(blockchain_name, mea, wallet_address, symbol))
    else:
        raise ValueError(approve_type)


if __name__ == '__main__':
    flags.DEFINE_string('blockchain_name', 'ethereum', 'Block chain name, ethereum by default.')
    flags.DEFINE_string('mea', None, 'mea: Spot.Uniswap.v2, Spot.Uniswap.v3 etc.')
    flags.DEFINE_string('wallet_address', None, 'Wallet address.')
    flags.DEFINE_string('symbol', None, 'Symbol, eg: ETH-USDC, ETH-USDC.5bp')
    flags.DEFINE_string('approve_type', None, 'Either swap or lp.')
    app.run(main)
